package ms.unicorn.language.languagedef;

import org.eclipse.draw2d.IFigure;

public class FigureContainer extends java.util.HashMap<String, Object> {
	
	private static final long serialVersionUID = 1L;
	private IFigure primaryFigure;

	public IFigure getMainFigure() {
		return this.getFigure("@main");
	}
	
	public void setMainFigure(IFigure figure) {
		this.put("@main", figure);
	}
	
	public IFigure getFigure(String name) {
		String[] dotName = name.split("\\.", 2);
		Object obj = get(dotName[0]);
		if (dotName.length > 1 && obj instanceof FigureContainer)
			return ((FigureContainer) obj).getFigure(dotName[1]);
		if (obj instanceof FigureContainer)
			obj = ((FigureContainer) obj).getMainFigure();
		return obj instanceof IFigure ? (IFigure)obj : null;
	}

	public void setPrimaryFigure(IFigure figure) {
		this.primaryFigure = figure;
	}
	
	public IFigure getPrimaryFigure() {
		return this.primaryFigure;
	}
	
	public IFigure resolvePrimaryFigure() {
		return this.primaryFigure != null ? this.primaryFigure : getMainFigure();
	}
}
